using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;

namespace Lin {
	public class MDAL {
		string _cs;
		public MDAL() : this("server=localhost;database=lin;uid=sysuser;pwd=syspass") { }
		public MDAL(params string[] args) {
			if(args.Length==2)
				_cs=string.Format("server={0};database={1};uid=sysuser;pwd=syspass",args);
			else
				throw new Exception();
		}

		public static string LastQry;

		public int NonQry(string sql,params object[] args) {
			string qry=sql;
			if(args.Length!=0)
				qry=string.Format(sql,args);
			LastQry=qry;
			int ret=-1;
			using(MySqlConnection con=new MySqlConnection(_cs))
			using(MySqlCommand cmd=new MySqlCommand(qry,con))
			{
				con.Open();
				ret=cmd.ExecuteNonQuery();
				con.Close();
			}
			return ret;
		}

		public MDAL(string cs) { _cs=cs; }
		public DataTable Query(string sql,params object[] args) {
			string qry=string.Format(sql,args);
			LastQry=qry;
			DataTable ret=new DataTable();
			using(MySqlDataAdapter adp=new MySqlDataAdapter(qry,_cs))
			{
				adp.SelectCommand.CommandTimeout=0;
				adp.Fill(ret);
			}
			return ret;
		}

		public object Scalar(string sql,params object[] args) {
			string qry=string.Format(sql,args);
			LastQry=qry;
			object ret=null;
			using(MySqlConnection con=new MySqlConnection(_cs))
			using(MySqlCommand cmd=new MySqlCommand(qry,con))
			{
				con.Open();
				ret=cmd.ExecuteScalar();
				con.Close();
			}
			return ret;
		}



	}
}
